<template>
  <pro-layout
    :title="title"
    :menus="menus"
    :collapsed="collapsed"
    :mediaQuery="query"
    :isMobile="isMobile"
    :handleMediaQuery="handleMediaQuery"
    :handleCollapse="handleCollapse"
    :i18nRender="i18nRender"
    v-bind="settings"
    :menuHeaderRender="menuHeaderRender"
  >
    <!-- 面包屑导航 -->
    <a-layout-content :style="{ height: '100%', margin: '0 0 40px 0', paddingTop: '0' }">
      <multi-tab v-if="multiTab"></multi-tab>
      <transition name="page-transition"> </transition>
    </a-layout-content>

    <!-- 右侧设置图标 -->
    <!-- <setting-drawer :settings="settings" @change="handleSettingChange" /> -->
    <SettingDrawer @change="handleSettingChange"></SettingDrawer>

    <!-- 顶部登录组件 -->
    <template v-slot:rightContentRender>
      <right-content :top-menu="settings.layout === 'topmenu'" :is-mobile="isMobile" :theme="settings.theme" />
    </template>

    <!-- 自定义顶部折叠按钮区域 collapsed button 的方法 -->
    <!-- <template v-slot:collapsedButtonRender>
    </template> -->

    <!-- 自定义标题 pageTitleRender -->

    <!-- 自定义菜单 menuRender-->

    <!-- 页脚 -->
    <template v-slot:footerRender>
      <global-footer />
    </template>

    <!-- 二维码 -->
    <PageFooter :collapsed="collapsed" />

    <!-- 页面路由占位符 -->
    <router-view />
  </pro-layout>
</template>

<script>
// import { SettingDrawer, updateTheme } from '@ant-design-vue/pro-layout'
import MultiTab from '@/components/MultiTab'
import SettingDrawer from '@/components/SettingDrawer'
import menuHeader from '@/components/menuHeader/menuHeader.vue'
import { updateTheme } from '@/components/SettingDrawer/settingConfig'
import { i18nRender } from '@/locales'
import { mapState } from 'vuex'
import { SIDEBAR_TYPE, TOGGLE_MOBILE_TYPE } from '@/store/mutation-types'

import defaultSettings from '@/config/defaultSettings'
import RightContent from '@/components/GlobalHeader/RightContent'
import GlobalFooter from '@/components/GlobalFooter'

export default {
  name: 'BasicLayout',
  components: {
    SettingDrawer,
    RightContent,
    GlobalFooter,
    MultiTab,
    menuHeader,
    PageFooter: () => import('./PageFooter.vue')
  },
  data () {
    return {
      // preview.pro.antdv.com only use.
      isProPreviewSite: process.env.VUE_APP_PREVIEW === 'true' && process.env.NODE_ENV !== 'development',
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      title: defaultSettings.title,
      multiTab: defaultSettings.multiTab,
      settings: {
        // 布局类型
        layout: this.$store.getters.layout, // 'sidemenu', 'topmenu'
        // 定宽: true / 流式: false
        contentWidth: this.$store.getters.contentWidth,
        // 主题 'dark' | 'light'
        theme: this.$store.getters.theme,
        // 主色调
        primaryColor: this.$store.getters.color,
        fixSiderbar: this.$store.getters.fixed_sidebar,
        colorWeak: this.$store.getters.weak,
        hideHintAlert: false,
        hideCopyButton: false
      },
      // 媒体查询
      query: {},

      // 是否手机模式
      isMobile: false,
      key: 0
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: state => state.permission.addRouters
    })
  },
  created () {
    const routes = this.mainMenu.find(item => item.path === '/')
    this.menus = (routes && routes.children) || []
    console.log('动态路由菜单', this.menus)
    // 处理侧栏收起状态
    this.$watch('collapsed', () => {
      this.$store.commit(SIDEBAR_TYPE, this.collapsed)
    })
    this.$watch('isMobile', () => {
      this.$store.commit(TOGGLE_MOBILE_TYPE, this.isMobile)
    })
  },
  mounted () {
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }

    // first update color
    // TIPS: THEME COLOR HANDLER!! PLEASE CHECK THAT!!
    if (process.env.NODE_ENV !== 'production' || process.env.VUE_APP_PREVIEW === 'true') {
      updateTheme(this.settings.primaryColor)
    }
  },
  methods: {
    // 自定义标题logo
    menuHeaderRender () {
      return <menuHeader settings={this.settings}></menuHeader>
    },
    i18nRender,
    handleMediaQuery (val) {
      this.query = val
      if (this.isMobile && !val['screen-xs']) {
        this.isMobile = false
        return
      }
      if (!this.isMobile && val['screen-xs']) {
        this.isMobile = true
        this.collapsed = false
        this.settings.contentWidth = false
        // this.settings.fixSiderbar = false
      }
    },
    handleCollapse (val) {
      this.collapsed = val
    },
    handleSettingChange ({ type, value }) {
      type && (this.settings[type] = value)
      switch (type) {
        case 'contentWidth':
          this.settings[type] = value === 'Fixed'
          break
        case 'layout':
          if (value === 'sidemenu') {
            this.settings.contentWidth = false
          } else {
            this.settings.fixSiderbar = false
            this.settings.contentWidth = true
          }
          break
      }
    }
  }
}
</script>

<style lang="less">
@import './BasicLayout.less';
</style>
